/**
 * 
 */
package net.sourceforge.simcpux;

import net.sourceforge.simcpux.uikit.SharedPreferencesHelper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

/**
 * @author forsecond
 * 
 */
public class UserSharedPreferencesActivity extends Activity implements OnClickListener, Constants {
    private EditText                userNickNameText, goodCountText, badCountText;
    private CheckBox                gameTypeCb;
    private Button                  settingBtn;
    private SharedPreferencesHelper sp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_info);

        sp = new SharedPreferencesHelper(this, VOTE_GAME_USER);

        userNickNameText = (EditText) findViewById(R.id.user_nickname);
        goodCountText = (EditText) findViewById(R.id.user_good_count_setting);
        badCountText = (EditText) findViewById(R.id.user_bad_count_setting);
        gameTypeCb = (CheckBox) findViewById(R.id.user_is_auto_setting_cb);
        settingBtn = (Button) findViewById(R.id.settingButton);
        initView();

        settingBtn.setOnClickListener(this);

    }

    private void initView() {
        String username = sp.getString(VOTE_GAME_USER_NAME);
        int goodCountDefault = sp.getInt(VOTE_GAME_USER_GOOD_COUNT_SETTING);
        int badCountDefault = sp.getInt(VOTE_GAME_USER_BAD_COUNT_SETTING);
        String gameType = sp.getString(VOTE_GAME_USER_GAME_TYPE);

        userNickNameText.setText(username);
        goodCountText.setText(goodCountDefault+"");
        badCountText.setText(badCountDefault+"");
        gameTypeCb.setChecked(VOTE_GAME_TYPE_AUTO.equals(gameType) ? true : false);
    }

    @Override
    public void onClick(View v) {
        sp.putString(VOTE_GAME_USER_NAME, userNickNameText.getText().toString());
        sp.putInt(VOTE_GAME_USER_GOOD_COUNT_SETTING,
            Integer.parseInt(goodCountText.getText().toString()));
        sp.putInt(VOTE_GAME_USER_BAD_COUNT_SETTING,
            Integer.parseInt(badCountText.getText().toString()));
        sp.putString(VOTE_GAME_USER_GAME_TYPE, gameTypeCb.isChecked() ? VOTE_GAME_TYPE_AUTO
            : VOTE_GAME_TYPE_NOT_AUTO);
        finish();

    }
}
